package com.guli.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> treeList() {
        //1.查询出所有的分类 java代码只和数据库做一次交互
        List<CategoryEntity> allList = baseMapper.selectList(null);
        //2.从所有的数据中筛选出一级分类
        //sreeam把对象变成流的形式遍历
        //filter表示过滤
        List<CategoryEntity>  l1List =  allList.stream().filter(item ->{
            //大括号中写具体的方法 return结果就是一级分类
            return item.getParentCid().equals(0L);
        }).map(l1 ->{
            //现在的l1就是一级分类
            List<CategoryEntity> l2List = getChildren(allList, l1);
            l1.setChildren(l2List);
            return l1;
        }).collect(Collectors.toList());
        return l1List;
    }


    private List<CategoryEntity> getChildren(List<CategoryEntity> allList,CategoryEntity category){

        List<CategoryEntity> l2List  = allList.stream().filter(item ->{
            return item.getParentCid().equals(category.getCatId());
        }).map(l2 ->{
            List<CategoryEntity> l3List = getChildren(allList, l2);
            l2.setChildren(l3List);
            return l2;
        }).collect(Collectors.toList());
        return l2List;
    }



//    @Override
//    public List<CategoryEntity> treeList() {
//        //在这需要查询一个树形列表
//        //1.查询所有的一级分类
//        QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
//        w1.eq("parent_cid",0);
//        List<CategoryEntity> l1List = baseMapper.selectList(w1);
//        //2.遍历所有的一级分类，根据每个一级分类查询二级分类
//        for (CategoryEntity l1 : l1List){
//            QueryWrapper<CategoryEntity> w2 = new QueryWrapper<>();
//            w2.eq("parent_cid",l1.getCatId());
//            //查询到了每个一级分类下的二级分类
//            List<CategoryEntity> l2List = baseMapper.selectList(w2);
//            l1.setChildren(l2List);
//            //3.遍历二级分类查询三级分类
//            for (CategoryEntity l2 : l2List){
//                QueryWrapper<CategoryEntity> w3 = new QueryWrapper<>();
//                w3.eq("parent_cid",l2.getCatId());
//                //查询到了二级分类下的三级分类
//                List<CategoryEntity> l3List = baseMapper.selectList(w3);
//                l2.setChildren(l3List);
//            }
//        }
//        return l1List;
//    }

}
